package com.xiebishe.mylibrary.common;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;

import com.xiebishe.mylibrary.R;
import com.xiebishe.mylibrary.base.MyApplication;


/**
 * @describe 自定义的toast
 */
public class LJToastUtils {

    public static Toast mToast;
//    private static final int TOP_OFFSET=DrawUtils.dip2px(55);

//    private static final int COLOR_BLACK=0xff6d6d6d;
//    private static final int COLOR_THEME=0xE5FF7963;
//    private static final int COLOR_RED=0xE5D91313;


//    public enum ColorType{
//        BLACK,THEME,RED
//    }

    public enum ImageType {
        NONE,           // 无图片
        EXCLAMATION,    // 感叹号
        TICK,           // 勾号
        CUSTOM          // 自定义
    }

    public static void showToast(@StringRes int resId, Object... args) {
        showToast(ResUtils.getString(resId, args));
    }

    public static void showToast(String msg) {
        showToast(ImageType.NONE, msg, Gravity.CENTER);
    }

    public static void showToast(ImageType imageType, String msg) {
        showToast(imageType, msg, Gravity.CENTER);
    }

    private static void showToast(ImageType imageType, String msg, int gravity) {
        if (mToast != null) {
            mToast.cancel();
        }

        View toastView = LayoutInflater.from(MyApplication.getInstance().getApplicationContext()).inflate(R.layout.layout_toast, null);
//        GradientDrawable drawable= (GradientDrawable)toastView.getBackground();
//        switch (colorType){
//            case BLACK:
//                drawable.setColor(COLOR_BLACK);
//                break;
//            case THEME:
//                drawable.setColor(COLOR_BLACK);
//                break;
//            case RED:
//                drawable.setColor(COLOR_BLACK);
//                break;
//        }
//
//        //图片
        ImageView imageView = toastView.findViewById(R.id.img_toast);

        switch (imageType) {
            case NONE:
                imageView.setVisibility(View.GONE);
                break;
            case EXCLAMATION:
                imageView.setImageResource(R.drawable.toast_exclamation);
                imageView.setVisibility(View.VISIBLE);
                break;
            case TICK:
                imageView.setImageResource(R.drawable.toast_tick);
                imageView.setVisibility(View.VISIBLE);
                break;
        }
        //字
        TextView textView = toastView.findViewById(R.id.tv_toast);
        textView.setText(msg);

        mToast = new Toast(MyApplication.getInstance().getApplicationContext());
        mToast.setView(toastView);
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.setGravity(gravity, 0, 0);
        mToast.show();
    }

    public static void showToastWithImage(@DrawableRes int resId, String msg) {
        showToastWithImage(resId, msg, Gravity.CENTER);
    }

    public static void showToastWithImage(@DrawableRes int resId, String msg, int gravity) {
        if (mToast != null) {
            mToast.cancel();
        }

        View toastView = LayoutInflater.from(MyApplication.getInstance().getApplicationContext()).inflate(R.layout.layout_toast, null);
        ImageView imageView = toastView.findViewById(R.id.img_toast);

        imageView.setImageResource(resId);
        imageView.setVisibility(View.VISIBLE);
        TextView textView = toastView.findViewById(R.id.tv_toast);
        textView.setText(msg);

        mToast = new Toast(MyApplication.getInstance().getApplicationContext());
        mToast.setView(toastView);
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.setGravity(gravity, 0, 0);
        mToast.show();
    }
}
